package com.tao.utilslib.os;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Administrator on 2017-09-28.
 */

public class AppInfoUtil {

    // 获取所有安装包信息
    public static List<PackageInfo> getPackageInfoList(Context context) {
        PackageManager pm = context.getPackageManager();
        return pm.getInstalledPackages(0);

    }
    //获取制定包信息
    public static PackageInfo getAppointPackageInfo(Context context, String packageName) {
        List<PackageInfo> packageInfoList = getPackageInfoList(context);
//        String packageName = context.getPackageName();
        for (PackageInfo info : packageInfoList) {
            if (packageName != null)
                if (info.packageName.equals(packageName)) {
                    return info;
                }
        }
        return null;
    }

    // 查看apk 信息
    public static PackageInfo getApkInfo(String apkPath, Context context) {
        PackageManager pm = context.getPackageManager();
        return pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    }

    /**
     * 获取当前应用的Application
     * 先使用ActivityThread里获取Application的方法，如果没有获取到，
     * 再使用AppGlobals里面的获取Application的方法
     * @return
     */
    public static Application getCurApplication(){
        Application application = null;
        try{
            Class atClass = Class.forName("android.app.ActivityThread");
            Method currentApplicationMethod = atClass.getDeclaredMethod("currentApplication");
            currentApplicationMethod.setAccessible(true);
            application = (Application) currentApplicationMethod.invoke(null);
            Log.d("fw_create","curApp class1:"+application);
        }catch (Exception e){
            Log.d("fw_create","e:"+e.toString());
        }

        if(application != null)
            return application;

        try{
            Class atClass = Class.forName("android.app.AppGlobals");
            Method currentApplicationMethod = atClass.getDeclaredMethod("getInitialApplication");
            currentApplicationMethod.setAccessible(true);
            application = (Application) currentApplicationMethod.invoke(null);
            Log.d("fw_create","curApp class2:"+application);
        }catch (Exception e){
            Log.d("fw_create","e:"+e.toString());
        }

        return application;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static ActivityManager.RunningAppProcessInfo getTopProcessInfo(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
        if (processes != null && processes.size() > 0) {
            for (ActivityManager.RunningAppProcessInfo info : processes) {
                if (info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return info;
                }
            }
        }
        return null;
    }


 
 
}
